package com.quark.admin.controller;

import com.google.gson.Gson;
import com.quark.admin.service.LabelService;
import com.quark.admin.service.PostsService;
import com.quark.common.base.BaseController;
import com.quark.common.dto.PageResult;
import com.quark.common.dto.QuarkResult;
import com.quark.common.entity.Label;
import com.quark.common.entity.Posts;
import com.quark.common.entity.PostsSummery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author LHR
 * Create By 2017/9/3
 */
@RequestMapping("/labels")
@RestController
public class LabelController extends BaseController {

    @Autowired
    private LabelService labelService;

    @Autowired
    private PostsService postsService;

    @GetMapping
    public PageResult getAll(String draw,
                             @RequestParam(required = false, defaultValue = "1") int start,
                             @RequestParam(required = false, defaultValue = "10") int length) {

        int pageNo = start / length;
        Page<Label> page = labelService.findByPage(pageNo, length);
        PageResult<List<Label>> result = new PageResult<>(draw,
                page.getTotalElements(),
                page.getTotalElements(),
                page.getContent());

        return result;
    }

    @PostMapping("/delete")
    public QuarkResult deleteLabels(@RequestParam(value = "id[]") Label[] id) {
        QuarkResult result = restProcessor(() -> {
            labelService.deleteInBatch(Arrays.asList(id));
            return QuarkResult.ok();
        });
        return result;
    }

    @PostMapping("/add")
    public QuarkResult addLabels(Label label){
        QuarkResult result = restProcessor(() -> {
            Label parentLabel = labelService.findOne(label.getParentId());
            if (parentLabel != null) {
                label.setTierLevel(parentLabel.getTierLevel() + 1);
            }
            labelService.save(label);
            return QuarkResult.ok();
        });

        return result;
    }

    @GetMapping("/update/HotContent/{labelid}")
    public QuarkResult updateHotContent(
            @PathVariable("labelid") Integer labelid) {

        QuarkResult result = restProcessor(() -> {
            Label label = labelService.findOne(labelid);
            if (label == null) return QuarkResult.error("标签不存在");
            Page<Posts> page = postsService.getPostsByTierLabel(label, 0, 5);
            Gson gson = new Gson();
            if (page.getContent() != null && page.getContent().size() > 0) {
                List<PostsSummery> postsSummeryList = new ArrayList<>();
                String newHotPostsIds = "";
                for (Posts posts : page.getContent()) {
                    PostsSummery postsSummery = new PostsSummery();
                    postsSummery.setId(posts.getId());
                    postsSummery.setTitle(posts.getTitle());
                    postsSummeryList.add(postsSummery);
                    newHotPostsIds = newHotPostsIds + posts.getId() + ",";
                }
               String hotJsonArray =  gson.toJson(postsSummeryList);
                if (!newHotPostsIds.equals(label.getHotPostsIds())) {
                    label.setHotContent(hotJsonArray);
                    label.setHotPostsIds(newHotPostsIds);
                    int newHotVersion = 0;
                    if (label.getHotVersion() != null) {
                        newHotVersion = label.getHotVersion();
                        newHotVersion ++;
                    }
                    if (newHotVersion > 1023) {
                        newHotVersion = 0;
                    }
                    label.setHotVersion(newHotVersion);
                    labelService.save(label);
                }
            }
            return QuarkResult.ok("ok");

        });

        return result;

    }

}
